<template>
  <div>
    <van-nav-bar
      title="更新昵称"
      left-text="取消"
      right-text="保存"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />

    <van-field
      v-model="nickname"
      rows="2"
      autosize
      type="textarea"
      maxlength="11"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { editUserProfileAPI } from '@/api'
export default {
  data() {
    return {
      nickname: this.name
    }
  },
  props: {
    name: {
      type: String,
      required: true
    }
  },
  methods: {
    onClickLeft() {
      this.$emit('close')
    },
    async onClickRight() {
      this.$toast.loading({
        message: '更新昵称中...',
        forbidClick: true
      })
      //1.发送请求 更新线上
      await editUserProfileAPI({
        name: this.nickname
      })
      //2.手动更新视图
      //sync.v-bind 指令修饰符
      //作用:更新绑定的值 绑定了一个自定义事件:update:属性
      this.$emit('update:name', this.nickname)
      this.$emit('close')
      this.$toast.success('更新昵称成功')
    }
  }
}
</script>

<style></style>
